/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package library.FileFilters;

import java.io.File;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.log4j.*;

/**
 * This file filter is to be user with File::listFiles.
 * <br>It inherits from java.io.FileFilter
 * (caution : there is a javax.swing.filechooser.FileFilter to use with JFileChooser).
 * <br>It makes it possible to list all the files :
 * <ul>
 * <li> whose filename matches one among a list of filename patterns (well-formed filenames),
 * <li> OR whose filename does not match any among a list of filename patterns (bad-formed filenames),
 * <li> including or not directories.
 * </ul>
 * @author JNC
 */
public class JpegFileFilter implements java.io.FileFilter {
   
    /**
     * The log4j log
     */
    @SuppressWarnings("unused")
	private static final Logger _logger = Logger.getLogger(JpegFileFilter.class);    
  
    /**
     * This indicator tells whether the directories should be considered to match the filter.
     */
    private final boolean _matchIfDirectory;
    /**
     * This indicators tells if the filter should match the well-formed filenames (true) or
     * bad-formed filename (false).
     */
    private final boolean _matchIfWellFormed;
    /**
     * List of patterns that defines well- and bad- formed filenames
     */
    private final List<String> _patterns;
    /**
     * List of extensions, among which the extension of the file must be
     */
    private final List<String> _extensions;
    
    /**
     * Constructor.
     * @param myMatchIfDirectory
     * Indicates whether the directories should be considered to match the filter or not.
     * @param myMatchIfWellFormed
     * Indicates whether the well-formed filename (true) or the bad-formed filename (false)
     * should be considered to match the filter.
     */
    public JpegFileFilter(boolean myMatchIfDirectory, boolean myMatchIfWellFormed) {
        _matchIfDirectory = myMatchIfDirectory;
        _matchIfWellFormed = myMatchIfWellFormed;
        _patterns = new ArrayList<String>();
        _extensions = new ArrayList<String>();
    }

    /**
     * Add a pattern to the list.
     * @param myPattern
     * The pattern to add to the list.
     */
    public void addPattern(String myPattern) {
        _patterns.add(myPattern);
    }
    
    /**
     * Add an extension to the list.
     * @param myExtension
     * The extension to add to the list
     */
    public void addExtension(String myExtension) {
        _extensions.add(myExtension);
    }
    
    /** This method indicates whether a file is considered to match the filter or not.
     * <br>Mandatory API when defining a file filter. 
     * <br>Tests are performed in the following order :
     * <ul>
     * <li> if the file is a directory, returns _includeWhenDirectory
     * <li> if at least one extension is defined, check the file extension
     * and returns false if the extension is not included in the extensions list
     * <li> if the filename matches one the specified pattern, returns _matchIfWellFormed
     * </ul>
     * @param myFile
     * The file to test.
     * @return
     * <ul>
     * <li> <b>true</b> if the file matches the filter
     * <li> <b>false</b> if the file does not match the filter
     * <ul/>
     */
    public boolean accept(File myFile) {

        boolean bReturn = ! _matchIfWellFormed;
        boolean bStop = false;
        
        // check directories
        // -----------------
        if (! bStop) {
            if (myFile.isDirectory()) {
                bStop = true;
                bReturn = _matchIfDirectory;            
            }
        }
        
        // check extensions
        // ----------------
        if (!bStop) {
            if (_extensions.size()>0) {
                boolean bBreakLoop = false;
                Iterator<String> iterator = _extensions.iterator();
                
                while (iterator.hasNext() && !bBreakLoop) {
                    if (myFile.getName().toLowerCase().endsWith((String)iterator.next())) {
                        bBreakLoop = true;                        
                    } // if the extension matches
                } // while
                
                if (!bBreakLoop) {
                    bStop = true;
                    bReturn = false;
                } // none of the extensions matches
            } // if _extensions.size <> 0
        } // if ! bStop

        // check patterns
        // --------------
        if (! bStop) {
            boolean bBreakLoop = false;
            Iterator<String> iterator = _patterns.iterator();
            
            while (iterator.hasNext() && !bBreakLoop) {
                if (myFile.getName().toLowerCase().matches((String)iterator.next())) {
                    bBreakLoop = true;
                    bStop = true;
                    bReturn =_matchIfWellFormed;
                }
            } // while
        }
        
        return bReturn;
        } // if myFile is a file
}